﻿using COMOSOFT.Class;
using DevExpress.Spreadsheet;
using DevExpress.XtraGrid.Views.Grid;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace COMOSOFT.HumanResource.Forms
{
    public partial class FrmPreview : Form
    {        
        ClassShare cls = new ClassShare("FrmPreview");
        GridView GV = null;
        public FrmPreview(GridView _GV)
        {
            InitializeComponent();
            this.GV = _GV;
        }
      
        public void LoadData()
        {
            MemoryStream sstream = new MemoryStream();
            DevExpress.XtraPrinting.XlsExportOptions xlsOption = new DevExpress.XtraPrinting.XlsExportOptions();
            xlsOption.TextExportMode = DevExpress.XtraPrinting.TextExportMode.Value;
            xlsOption.ShowGridLines = false;
            xlsOption.ExportMode = DevExpress.XtraPrinting.XlsExportMode.SingleFile;

            GV.ExportToXls(sstream, xlsOption);
           spreadsheet_Preview.LoadDocument(sstream, DocumentFormat.Xls);
           spreadsheet_Preview.Document.Worksheets.ActiveWorksheet.Columns.AutoFit(1, 100);
           spreadsheet_Preview.Document.Worksheets.ActiveWorksheet.Rows.AutoFit(1, 100);
           spreadsheet_Preview.Document.Worksheets.ActiveWorksheet.Rows[17].Alignment.WrapText = true;
            for (int i = 0; i < 8; i++)
            {
               spreadsheet_Preview.ActiveWorksheet.Rows.Insert(i, 1);
            }
            //if (!File.Exists(ClassShare.lcSystem + "logo.PNG"))
            //    cls.Dowload(ClassShare.ftpSystem, ClassShare.lcSystem, "logo.PNG");
            //spreadsheet_Preview.ActiveWorksheet.Shapes.AddPicture(ClassShare.lcSystem + "logo.PNG", 50, 50, 1200, 300, false);
           spreadsheet_Preview.ActiveWorksheet.Rows[5][9].SetValue("EMPLOYEE LIST");
           spreadsheet_Preview.ActiveWorksheet.Rows[5][9].Font.UnderlineType = UnderlineType.Single;
           spreadsheet_Preview.ActiveWorksheet.Rows[5][9].Font.Bold = true;
           spreadsheet_Preview.ActiveWorksheet.Rows[5][9].Font.Color = Color.Blue;
           spreadsheet_Preview.ActiveWorksheet.Rows[5][9].Font.Size = 18;
           spreadsheet_Preview.ActiveWorksheet.Range["G6:M6"].FillColor = Color.LightGray;
        }

        private void dataNavigator_Preview_ButtonClick(object sender, DevExpress.XtraEditors.NavigatorButtonClickEventArgs e)
        {
            if (e.Button.Tag.ToString() == "Print")
            {
                spreadsheet_Preview.ShowPrintDialog();
            }
            else
            {
                if (e.Button.Tag.ToString() == "Save")
                {
                    String fileName = null;
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel 2003|*.xls|Excel 2007|*.xlsx";
                    sfd.Title = "Save an Excel File";
                    sfd.FileName = "EmployeeList";
                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        try
                        {
                            fileName = sfd.FileName;
                            spreadsheet_Preview.SaveDocument(fileName, DocumentFormat.Xls);
                            DialogResult dlr = MessageBox.Show("Open File Export", "Do you want to open it?",
                                               MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                            if (dlr == DialogResult.Yes)
                            {
                                System.Diagnostics.Process.Start(fileName);
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                }
                else
                    LoadData();
            }
        }
    }
}
